home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / kernel / object.d < prev    next >
Text File  |  1997-04-12  |  4KB  |  206 lines

  1. /*
  2.  *
  3.  *    Copyright (c) 1993-1996 Algorithms Corporation
  4.  *    3020 Liberty Hills Drive
  5.  *    Franklin, TN  37067
  6.  *
  7.  *    ALL RIGHTS RESERVED.
  8.  *
  9.  *
  10.  *
  11.  */
  12.  
  13. #include <math.h>
  14.  
  15. public    defclass Object  {
  16.     object         cls;
  17.     unsigned short    tag;    /*  info tags        */
  18.     unsigned short    siz;    /*  used by GC        */
  19. };
  20.  
  21. imeth    gError(char *msg)
  22. {
  23.     if (File_c)
  24.         gPuts(stderrStream, msg);
  25.     else  {
  26.         if (IsObj((object) msg))
  27.             msg = gStringValue((object) msg);
  28.         fprintf(stderr, "%s", msg);
  29.     }
  30.     exit(1);
  31.     return self;   /*  if it did return  */
  32. }
  33.  
  34. ivmeth    vError(char *fmt, ...)
  35. {
  36.     char    buf[512];
  37.     MAKE_REST(fmt);
  38.  
  39.     vsprintf(buf, fmt, _rest_);
  40.     return gError(self, buf);
  41. }
  42.  
  43. imeth    gSubclassResponsibility(char *meth)
  44. {
  45.     char    buf[100];
  46.     object    aCls;
  47.  
  48.     aCls = IsaClass(self) ? self : ClassOf(self);
  49.     sprintf(buf, "Method %s should be implemented by a subclass of %s\n", 
  50.         meth, gName(aCls));
  51.     return gError(self, buf);
  52. }
  53.  
  54. imeth    gShouldNotImplement(char *meth)
  55. {
  56.     char    buf[100];
  57.  
  58.     if (IsaClass(self))
  59.         sprintf(buf, "Method %s is not appropriate for the %s class\n", 
  60.             meth, gName(self));
  61.     else
  62.         sprintf(buf, "Method %s is not appropriate for instances of %s\n", 
  63.             meth, gName(ClassOf(self)));
  64.     return gError(self, buf);
  65. }
  66.  
  67. #ifndef    NOCLASSLIB
  68.  
  69. imeth    gStringRep()
  70. {
  71.     object    s, t;
  72.  
  73.     s = vSprintf(String, "%s:<%8.8lx> [ ", gName(ClassOf(self)), self);
  74.     t = gStringRepValue(self);
  75.     vBuild(s, NULL, t, " ]\n", NULL);
  76.     gDispose(t);
  77.     return s;
  78. }
  79.  
  80. imeth    gStringRepValue()
  81. {
  82.     return vSprintf(String, "%s:<%8.8lx>", gName(ClassOf(self)), self);
  83. }
  84.  
  85. imeth    gPrint(object stream)
  86. {
  87.     object    t;
  88.  
  89.     ChkArg(stream, 2);
  90.     t = gStringRep(self);
  91.     gPuts(stream, (char *) t);
  92.     gDispose(t);
  93.     return self;
  94. }
  95.  
  96. imeth    gPrintValue(object stream)
  97. {
  98.     object    t;
  99.  
  100.     ChkArg(stream, 2);
  101.     t = gStringRepValue(self);
  102.     gPuts(stream, (char *) t);
  103.     gDispose(t);
  104.     return self;
  105. }
  106.  
  107. #endif
  108.  
  109. imeth    int    gHash()
  110. {
  111.     double    t;
  112.  
  113.     t = .6125423371    * (double)(unsigned long) self;
  114.     t = t < 0.0 ? -t : t;
  115.     return (int) (BIG_INT * (t - floor(t)));
  116. }
  117.  
  118. imeth    int    gCompare(object obj2)
  119. {
  120.     if (EQ(self, obj2))
  121.         return 0;
  122.     ChkArg(obj2, 2);
  123.     if ((unsigned long) self < (unsigned long) obj2)
  124.         return -1;
  125.     else
  126.         return 1;
  127. }
  128.  
  129. imeth    int    gIsKindOf(object aCls)
  130. {
  131.     static    gIsKindOf_t    class_IsKindOf = NULL;
  132.  
  133.     /*  aCls need not be validated  */
  134.  
  135.     if (!class_IsKindOf)
  136.         class_IsKindOf = imcPointer(Behavior, gIsKindOf);
  137.     return class_IsKindOf(ClassOf(self), aCls);
  138. }
  139.  
  140. imeth    gInit()
  141. {
  142.     return self;
  143. }
  144.  
  145. objrtn    Object_initialize(void)
  146. {
  147.     static    int    done = 0;
  148.  
  149.     /*  Class creation and some of the methods are initialized by
  150.         the kernel  */
  151.  
  152.     if (done)
  153.         return Object_c;
  154.  
  155.     done = 1;
  156.  
  157. /*    Object_c = gNewClass(Class, "Object", sizeof(Object_iv_t), 0, END);    */
  158.  
  159.     iMethodFor(Object, gError, Object_im_gError);
  160. #if    DPP_STRATEGY == 1
  161.     ivMethodFor(Object, vError, Object_ivm_vError, Object_ivm_vError);
  162. #else
  163.     ivMethodFor(Object, vError, Object_ivm_vError, Object_ifm_vError);
  164. #endif
  165.     iMethodFor(Object, gSubclassResponsibility, Object_im_gSubclassResponsibility);
  166.     iMethodFor(Object, gShouldNotImplement, Object_im_gShouldNotImplement);
  167.     iMethodFor(Object, gHash, Object_im_gHash);
  168.     iMethodFor(Object, gCompare, Object_im_gCompare);
  169.     iMethodFor(Object, gIsKindOf, Object_im_gIsKindOf);
  170.     iMethodFor(Object, gInit, Object_im_gInit);
  171.  
  172. #ifndef    NOCLASSLIB
  173.     iMethodFor(Object, gStringRep, Object_im_gStringRep);
  174.     iMethodFor(Object, gStringRepValue, Object_im_gStringRepValue);
  175.     iMethodFor(Object, gPrint, Object_im_gPrint);
  176.     iMethodFor(Object, gPrintValue, Object_im_gPrintValue);
  177. #endif
  178.     return Object_c;
  179. }
  180.  
  181. #if 0  /*  code for the benefit of dpp  */
  182.  
  183. imeth    gDispose, gDeepDispose, gGCDispose (){}
  184. imeth    int    gEqual(object obj2){}
  185. imeth    gCopy, gDeepCopy (){}
  186. imeth    int    gSize(){}
  187. imeth    int    gBasicSize(){}
  188.  
  189.  
  190.  
  191.  
  192. #endif
  193.  
  194.  
  195. /*
  196.  *
  197.  *    Copyright (c) 1993-1996 Algorithms Corporation
  198.  *    3020 Liberty Hills Drive
  199.  *    Franklin, TN  37067
  200.  *
  201.  *    ALL RIGHTS RESERVED.
  202.  *
  203.  *
  204.  *
  205.  */
  206.